using System.Text.RegularExpressions;
using SS.AspNet.UrlRewriter.Config;
using SS.Common.Logging;

namespace SS.AspNet.UrlRewriter
{
	/// <summary>
	/// Provides a rewriting HttpModule.
	/// </summary>
	public class ModuleRewriter : BaseModuleRewriter
	{
        private static readonly ILogger Logger = LoggerManager.GetLogger(typeof(ModuleRewriter).Name);

		/// <summary>
		/// This method is called during the module's BeginRequest event.
		/// </summary>
        /// <param name="requestedPath">The RawUrl being requested (includes path and querystring).</param>
		/// <param name="app">The HttpApplication instance.</param>
		protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
		{
            // log information to the Trace object.
            Logger.Debug("Entering ModuleRewriter");

            // get the configuration rules
            RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;

            // iterate through each rule...
            for (int i = 0; i < rules.Count; i++)
            {
                // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
                string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";

                // Create a regex (note that IgnoreCase is set...)
                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);

                // See if a match is found
                if (re.IsMatch(requestedPath))
                {
                    if (requestedPath.ToUpper().Contains("ADMINCP") || requestedPath.Contains("errorsyes.aspx"))
                    {
                        break;
                    }
                    // match found - do any replacement needed
                    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));

                    // log rewriting information to the Trace object
                    Logger.Debug("Rewriting URL to " + sendToUrl);

                    // Rewrite the URL
                    RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                    break;		// exit the for loop
                }
            }

            // Log information to the Trace object
            Logger.Debug("Exiting ModuleRewriter");
		}
	}
}
